package com.mjf.srm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.srm.domain.SrmpurFind;
import com.mjf.srm.domain.SrmpurList;
import com.mjf.srm.mapper.SrmpurFindMapper;
import com.mjf.srm.service.SRMAsyncService;
import com.mjf.srm.service.SrmpurFindService;
import com.mjf.srm.service.SrmpurListService;
import com.mjf.srm.vo.PURInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/29 02:06
 */
@Service
public class SrmpurFindServiceImpl extends ServiceImpl<SrmpurFindMapper, SrmpurFind> implements SrmpurFindService {


    @Autowired
    private SRMAsyncService srmAsyncService;
    @Autowired
    private SrmpurListService srmpurListService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncCGDD() {
        List<PURInfoVO.PURVO> purvos = srmAsyncService.asyncPurchaseOrder();
        // 查询当天的采购订单
        List<SrmpurList> alSrmpurList =
            srmpurListService.list(Wrappers.<SrmpurList>lambdaQuery().eq(SrmpurList::getReleaseDate, LocalDate.now()));
        List<String> alIds = alSrmpurList.stream().map(SrmpurList::getPurHeaderId).collect(Collectors.toList());
        // 采购订单详情
        List<SrmpurFind> alSrmpurFinds = list(Wrappers.<SrmpurFind>lambdaQuery().eq(SrmpurFind::getReleaseDate,
            LocalDate.now()));
        List<String> alPurIds = alSrmpurFinds.stream().map(SrmpurFind::getPurLineId).collect(Collectors.toList());

        if (CollectionUtil.isNotEmpty(purvos)) {
            List<SrmpurList> srmpurLists = new ArrayList<>();
            List<SrmpurFind> srmpurFinds = new ArrayList<>();
            for (PURInfoVO.PURVO purVO : purvos) {
                SrmpurList srmpurList = new SrmpurList();
                srmpurList.setPurHeaderId(purVO.getPUR_HEADER_ID());
                srmpurList.setGetpurfind("1");
                srmpurList.setVendorCode(purVO.getVENDOR_CODE());
                srmpurList.setVendorName(purVO.getVENDOR_NAME());
                srmpurList.setDisplayPoNumber(purVO.getDISPLAY_PO_NUMBER());
                srmpurList.setSumAmount(purVO.getSUM_AMOUNT());
                srmpurList.setCurrencyCode(purVO.getCURRENCY_CODE());
                srmpurList.setAgentName(purVO.getAGENT_NAME());
                if (StrUtil.isNotBlank(purVO.getRELEASE_DATE())) {
                    srmpurList.setReleaseDate(LocalDate.parse(purVO.getRELEASE_DATE()));
                }
                srmpurList.setTypeLookupDesc(purVO.getTYPE_LOOKUP_DESC());
                // 采购单号
                if (!alIds.contains(srmpurList.getPurHeaderId())) {
                    srmpurLists.add(srmpurList);
                    alIds.add(srmpurList.getPurHeaderId());
                }
                // 明细
                SrmpurFind srmpurFind = new SrmpurFind();
                srmpurFind.setPurHeaderId(srmpurList.getPurHeaderId());
                srmpurFind.setVendorCode(purVO.getVENDOR_CODE());
                srmpurFind.setVendorName(purVO.getVENDOR_NAME());
                srmpurFind.setDisplayPoNumber(purVO.getDISPLAY_PO_NUMBER());
                if (ObjectUtil.isNotNull(purVO.getQUANTITY()) && ObjectUtil.isNotNull(purVO.getUNIT_PRICE())) {
                    srmpurFind.setAmount(purVO.getQUANTITY().multiply(purVO.getUNIT_PRICE()));
                }
                srmpurFind.setCurrencyCode(purVO.getCURRENCY_CODE());
                srmpurFind.setAgentName(purVO.getAGENT_NAME());
                if (StrUtil.isNotBlank(purVO.getRELEASE_DATE())) {
                    srmpurFind.setReleaseDate(LocalDate.parse(purVO.getRELEASE_DATE()));
                }
                srmpurFind.setTypeLookupDesc(purVO.getTYPE_LOOKUP_DESC());
                srmpurFind.setItemCode(purVO.getITEM_CODE());
                srmpurFind.setItemDescription(purVO.getITEM_DESCRIPTION());
                srmpurFind.setSizeDimension(purVO.getSIZE_DIMENSION());
                srmpurFind.setMaterialSpec(purVO.getMATERIAL_SPEC());
                srmpurFind.setQuantity(purVO.getQUANTITY());
                srmpurFind.setUnitMeasLookupCode(purVO.getUNIT_MEAS_LOOKUP_CODE());
                srmpurFind.setUnitPrice(purVO.getUNIT_PRICE());
                srmpurFind.setEachNumber(purVO.getEACH_NUMBER());
                srmpurFind.setProjectCode(purVO.getPROJECT_CODE());
                srmpurFind.setProjectName(purVO.getPROJECT_NAME());
                srmpurFind.setStatusDesc(purVO.getSTATUS_DESC());
                srmpurFind.setPurLineId(purVO.getPUR_LINE_ID());
                srmpurFind.setJhljsl(purVO.getJHLJSL());
                srmpurFind.setFpljje(purVO.getFPLJJE());
                srmpurFind.setZbtzsbh(purVO.getZBTZSBH());
                srmpurFind.setZsydbs(purVO.getZSYDBS());
                srmpurFind.setZskuwei(purVO.getZSKUWEI());
                srmpurFind.setLineNum(purVO.getLINE_NUM());
                if (!alPurIds.contains(srmpurFind.getPurLineId())) {
                    srmpurFinds.add(srmpurFind);
                    alPurIds.add(srmpurFind.getPurLineId());
                }
            }
            if (CollectionUtil.isNotEmpty(srmpurLists)) {
                srmpurListService.saveBatch(srmpurLists);
            }
            if (CollectionUtil.isNotEmpty(srmpurFinds)) {
                saveBatch(srmpurFinds);
            }
        }
    }
}
